const CloneDeep = require('../template')

console.log("=== CloneDeep 基础测试 ===")
// 测试基本类型深拷贝
console.log(
  "基本类型深拷贝:", (() => {
    const num = 42
    const str = "hello"
    const bool = true
    const nul = null
    const undef = undefined

    return CloneDeep.cloneDeep(num) === num &&
      CloneDeep.cloneDeep(str) === str &&
      CloneDeep.cloneDeep(bool) === bool &&
      CloneDeep.cloneDeep(nul) === nul &&
      CloneDeep.cloneDeep(undef) === undef
  })()
)

// 测试数组深拷贝
console.log(
  "数组深拷贝:", (() => {
    const arr = [1, 2, [3, 4], { a: 5 }]
    const cloned = CloneDeep.cloneDeep(arr)

    return Array.isArray(cloned) &&
      cloned.length === 4 &&
      cloned[0] === 1 &&
      cloned[1] === 2 &&
      Array.isArray(cloned[2]) &&
      cloned[2][0] === 3 &&
      cloned[2][1] === 4 &&
      typeof cloned[3] === 'object' &&
      cloned[3].a === 5 &&
      cloned !== arr &&
      cloned[2] !== arr[2] &&
      cloned[3] !== arr[3]
  })()
)

// 测试对象深拷贝
console.log(
  "对象深拷贝:", (() => {
    const obj = { a: 1, b: { c: 2 }, d: [3, 4] }
    const cloned = CloneDeep.cloneDeep(obj)

    return typeof cloned === 'object' &&
      cloned.a === 1 &&
      typeof cloned.b === 'object' &&
      cloned.b.c === 2 &&
      Array.isArray(cloned.d) &&
      cloned.d[0] === 3 &&
      cloned.d[1] === 4 &&
      cloned !== obj &&
      cloned.b !== obj.b &&
      cloned.d !== obj.d
  })()
)

// 测试嵌套对象深拷贝
console.log(
  "嵌套对象深拷贝:", (() => {
    const obj = {
      level1: {
        level2: {
          level3: {
            value: 'deep'
          }
        }
      }
    }
    const cloned = CloneDeep.cloneDeep(obj)

    return cloned.level1.level2.level3.value === 'deep' &&
      cloned.level1 !== obj.level1 &&
      cloned.level1.level2 !== obj.level1.level2 &&
      cloned.level1.level2.level3 !== obj.level1.level2.level3
  })()
)

// 测试数组对象混合深拷贝
console.log(
  "数组对象混合深拷贝:", (() => {
    const data = {
      users: [
        { id: 1, name: 'Alice', hobbies: ['reading', 'coding'] },
        { id: 2, name: 'Bob', hobbies: ['gaming'] }
      ],
      meta: { count: 2 }
    }
    const cloned = CloneDeep.cloneDeep(data)

    return Array.isArray(cloned.users) &&
      cloned.users.length === 2 &&
      cloned.users[0].id === 1 &&
      cloned.users[0].name === 'Alice' &&
      Array.isArray(cloned.users[0].hobbies) &&
      cloned.users[0].hobbies[0] === 'reading' &&
      cloned.users[0].hobbies[1] === 'coding' &&
      cloned.users[1].id === 2 &&
      cloned.users[1].name === 'Bob' &&
      Array.isArray(cloned.users[1].hobbies) &&
      cloned.users[1].hobbies[0] === 'gaming' &&
      cloned.meta.count === 2 &&
      cloned !== data &&
      cloned.users !== data.users &&
      cloned.users[0] !== data.users[0] &&
      cloned.users[0].hobbies !== data.users[0].hobbies
  })()
)

// 测试日期对象深拷贝
console.log(
  "日期对象深拷贝:", (() => {
    const date = new Date('2023-01-01')
    const cloned = CloneDeep.cloneDeep(date)

    return cloned instanceof Date &&
      cloned.getTime() === date.getTime() &&
      cloned !== date
  })()
)

// 测试正则表达式深拷贝
console.log(
  "正则表达式深拷贝:", (() => {
    const regex = /test/gi
    const cloned = CloneDeep.cloneDeep(regex)

    return cloned instanceof RegExp &&
      cloned.source === regex.source &&
      cloned.flags === regex.flags &&
      cloned !== regex
  })()
)

// 测试函数深拷贝
console.log(
  "函数深拷贝:", (() => {
    const func = function test() { return 'hello' }
    const cloned = CloneDeep.cloneDeep(func)

    return typeof cloned === 'function' &&
      cloned.name === func.name &&
      cloned() === func()
  })()
)

// 测试空对象深拷贝
console.log(
  "空对象深拷贝:", (() => {
    const emptyObj = {}
    const cloned = CloneDeep.cloneDeep(emptyObj)

    return typeof cloned === 'object' &&
      Object.keys(cloned).length === 0 &&
      cloned !== emptyObj
  })()
)

// 测试空数组深拷贝
console.log(
  "空数组深拷贝:", (() => {
    const emptyArr = []
    const cloned = CloneDeep.cloneDeep(emptyArr)

    return Array.isArray(cloned) &&
      cloned.length === 0 &&
      cloned !== emptyArr
  })()
)

// 测试Symbol深拷贝
console.log(
  "Symbol深拷贝:", (() => {
    const sym = Symbol('test')
    const cloned = CloneDeep.cloneDeep(sym)

    return typeof cloned === 'symbol' &&
      cloned.toString() === sym.toString()
  })()
)

// 测试BigInt深拷贝
console.log(
  "BigInt深拷贝:", (() => {
    const bigInt = BigInt(123)
    const cloned = CloneDeep.cloneDeep(bigInt)

    return typeof cloned === 'bigint' &&
      cloned === bigInt
  })()
)

// 测试Map深拷贝
console.log(
  "Map深拷贝:", (() => {
    const map = new Map([['a', 1], ['b', 2]])
    const cloned = CloneDeep.cloneDeep(map)

    return cloned instanceof Map &&
      cloned.size === 2 &&
      cloned.get('a') === 1 &&
      cloned.get('b') === 2 &&
      cloned !== map
  })()
)

// 测试Set深拷贝
console.log(
  "Set深拷贝:", (() => {
    const set = new Set([1, 2, 3])
    const cloned = CloneDeep.cloneDeep(set)

    return cloned instanceof Set &&
      cloned.size === 3 &&
      cloned.has(1) &&
      cloned.has(2) &&
      cloned.has(3) &&
      cloned !== set
  })()
)

// 测试循环引用深拷贝
console.log(
  "循环引用深拷贝:", (() => {
    const obj = { a: 1 }
    obj.self = obj

    try {
      const cloned = CloneDeep.cloneDeep(obj)
      return cloned.a === 1 &&
        cloned.self === cloned &&
        cloned !== obj
    } catch (error) {
      return true // 循环引用应该抛出错误或正确处理
    }
  })()
)

// 测试原型链深拷贝
console.log(
  "原型链深拷贝:", (() => {
    function TestClass() {
      this.value = 'test'
    }
    TestClass.prototype.method = function() { return 'method' }

    const instance = new TestClass()
    const cloned = CloneDeep.cloneDeep(instance)

    return cloned.value === 'test' &&
      typeof cloned.method === 'function' &&
      cloned.method() === 'method' &&
      cloned !== instance
  })()
)

// 测试undefined属性深拷贝
console.log(
  "undefined属性深拷贝:", (() => {
    const obj = { a: 1, b: undefined, c: 3 }
    const cloned = CloneDeep.cloneDeep(obj)

    return cloned.a === 1 &&
      cloned.b === undefined &&
      cloned.c === 3 &&
      'b' in cloned &&
      cloned !== obj
  })()
)

// 测试null属性深拷贝
console.log(
  "null属性深拷贝:", (() => {
    const obj = { a: 1, b: null, c: 3 }
    const cloned = CloneDeep.cloneDeep(obj)

    return cloned.a === 1 &&
      cloned.b === null &&
      cloned.c === 3 &&
      cloned !== obj
  })()
)

// 测试复杂嵌套结构深拷贝
console.log(
  "复杂嵌套结构深拷贝:", (() => {
    const complex = {
      arr: [1, { nested: 2 }, [3, 4]],
      obj: {
        deep: {
          value: 'test',
          arr: [5, 6]
        }
      },
      primitives: {
        str: 'string',
        num: 42,
        bool: true
      }
    }
    const cloned = CloneDeep.cloneDeep(complex)

    return Array.isArray(cloned.arr) &&
      cloned.arr[0] === 1 &&
      typeof cloned.arr[1] === 'object' &&
      cloned.arr[1].nested === 2 &&
      Array.isArray(cloned.arr[2]) &&
      cloned.arr[2][0] === 3 &&
      cloned.arr[2][1] === 4 &&
      typeof cloned.obj === 'object' &&
      typeof cloned.obj.deep === 'object' &&
      cloned.obj.deep.value === 'test' &&
      Array.isArray(cloned.obj.deep.arr) &&
      cloned.obj.deep.arr[0] === 5 &&
      cloned.obj.deep.arr[1] === 6 &&
      cloned.primitives.str === 'string' &&
      cloned.primitives.num === 42 &&
      cloned.primitives.bool === true &&
      cloned !== complex &&
      cloned.arr !== complex.arr &&
      cloned.arr[1] !== complex.arr[1] &&
      cloned.arr[2] !== complex.arr[2] &&
      cloned.obj !== complex.obj &&
      cloned.obj.deep !== complex.obj.deep &&
      cloned.obj.deep.arr !== complex.obj.deep.arr
  })()
)

console.log("=== CloneDeep 基础测试完成 ===")
